import router from '@/router/index'
import api from '@/api/common'
import {Message} from "element-ui";
import menu from './menu.js'

export default {
    namespaced: true,
    state: {
        tokenInfo: JSON.parse(sessionStorage.getItem('tokenInfo')) || null,
        userInfo: JSON.parse(sessionStorage.getItem('userInfo')) || null,
        userRouteList: JSON.parse(sessionStorage.getItem('userRouteList')),
        routerPath:'/home'
    },
    mutations: {
        setUserInfo(state, res) {
            state.tokenInfo = res.tokenInfo
            state.userInfo = res.userInfo
            sessionStorage.setItem('tokenInfo',JSON.stringify( res.tokenInfo))
            sessionStorage.setItem('userInfo', JSON.stringify( res.userInfo))
        },
        setRouter(state, value) {
            state.userRouteList = value
        },
        setRouterPath(state, value) {
            state.routerPath = value
        },
    },
    actions: {
        // 1.登录获取用户信息,存本地和store
        ayLogin({commit, dispatch}, res) {
            commit('setUserInfo', res)
            dispatch('getRouter')
        },
        getRouter({commit, dispatch, state}) {
            // api.getMenuList().then(res => {
            let res = menu;
                let btnList = []
                let statusRoutes  = []
                for (let i in res){
                    if (res[i].menuType == 'C' && res[i].visible == '0'){
                        commit('setRouterPath',res[i].path)
                        break;
                    }
                }
                res.forEach(item=>{
                    if (item.menuType == "F"){
                        btnList.push(item.perms)
                    }
                   else if(item.status == 1 ){
                        statusRoutes.push(item.path)
                    }
                })
                sessionStorage.setItem('roleMetaBtn', JSON.stringify(btnList))
                sessionStorage.setItem('statusRoutes', JSON.stringify(statusRoutes))
                function  getTree(res) {
                    const tree = [];
                    const idMap = new Map();
                    res.forEach(item => {
                        idMap.set(item.menuId, item);
                    });
                    res.forEach(item => {
                           if (idMap.has(item.parentId)) {
                               const parent = idMap.get(item.parentId);
                               if (!parent.children) {
                                   parent.children = [];
                               }
                               parent.children.push(item);
                           } else {
                               tree.push(item);
                           }
                    });
                    return tree;
                }
                let sortArr = getTree(res)
                function sortArrFn(sortArr){
                    sortArr.forEach(item=>{
                        if ((item.children && item.children.length > 0) && item.menuType != 'F'){
                            sortArrFn(item.children)
                        }
                    })
                    for (let i = 0; i < sortArr.length-1; i++) {
                        let minIndex = i; //用于存储最小值的下标，排序刚开始时，假设最小值的下标就是选中的基准数据下标
                        for (let j = i+1; j < sortArr.length; j++) {
                            if (sortArr[j].orderNum < sortArr[minIndex].orderNum) {
                                minIndex = j;
                            }
                        }
                        [sortArr[i],sortArr[minIndex]] = [sortArr[minIndex],sortArr[i]]
                    }
                }
                sortArrFn(sortArr )
                let ayRouters = sortArr
                commit('setRouter', ayRouters)
                sessionStorage.setItem('userRouteList', JSON.stringify(ayRouters))
                router.push( sessionStorage.getItem('navActive')  || '/'+ state.routerPath).catch(err => {
                })
            // })
        },

    }
}

